<script lang="ts" setup>
import Avatar from '@/libs/Avatar/Avatar.vue'
import { formatMessageTime } from '@/utils/dateUtils'

interface Session {
  id: string
  type: 'private' | 'group'
  name: string
  avatar?: string
  unreadCount: number
  lastActiveTime: Date
  participants: string[]
  lastMessage?: {
    type: string
    content: string
    timestamp: Date
  }
}

interface Props {
  session: Session
  isActive?: boolean
}

const props = defineProps<Props>()

const emit = defineEmits<{
  click: [sessionId: string]
}>()

// 处理会话点击
const handleClick = (): void => {
  emit('click', props.session.id)
}

// 获取最后一条消息的预览文本
const getMessagePreview = (message: { type: string; content: string } | undefined): string => {
  if (!message) return '暂无消息'
  switch (message.type) {
    case 'text':
      return message.content
    case 'image':
      return '[图片]'
    case 'voice':
      return '[语音]'
    case 'video':
      return '[视频]'
    case 'file':
      return '[文件]'
    default:
      return '[消息]'
  }
}
</script>

<template>
  <div class="session-item" :class="{ active: props.isActive }" @click="handleClick">
    <div class="session-avatar">
      <Avatar
        :src="session.avatar"
        :alt="session.name"
        :username="session.name"
        status="online"
        size="medium"
      />
    </div>
    <div class="session-info">
      <div class="session-header">
        <span class="session-name">{{ session.name }}</span>
        <span class="session-time">
          {{ session.lastMessage ? formatMessageTime(session.lastMessage.timestamp) : '' }}
        </span>
      </div>
      <div class="session-footer">
        <span class="session-preview">
          {{ getMessagePreview(session.lastMessage) }}
        </span>
        <div v-if="session.unreadCount > 0" class="unread-badge">
          {{ session.unreadCount > 99 ? '99+' : session.unreadCount }}
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.session-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.session-item:hover {
  background-color: var(--hover-bg-color);
}

.session-item.active {
  background-color: var(--primary-color-light, rgba(24, 144, 255, 0.1));
  border-left: 3px solid var(--primary-color, #1890ff);
}

.session-item.active:hover {
  background-color: var(--primary-color-light, rgba(24, 144, 255, 0.15));
}

.session-avatar {
  margin-right: 16px;
}

.session-info {
  flex-grow: 1;
}

.session-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.session-name {
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-time {
  font-size: 12px;
  color: var(--text-muted-color);
}

.session-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative; /* 确保子元素可以相对于它定位 */
}

.session-preview {
  font-size: 12px;
  color: var(--text-muted-color);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-grow: 1;
  margin-right: 8px;
  max-width: 160px; /* 固定最大宽度，确保时间有足够空间 */
  width: auto; /* 允许宽度自适应，但不超过max-width */
  display: inline-block; /* 确保宽度限制生效 */
}

.unread-badge {
  background-color: var(--primary-color);
  color: white;
  border-radius: 10px;
  padding: 2px 6px;
  min-width: 18px;
  font-size: 10px;
  text-align: center;
  font-weight: 500;
}
</style>
